<template>
    <div class="singer" ref="singer">
        <list-view :data="singers" @selectedItem="selectedSinger" ref="list"></list-view>
        <router-view></router-view>
    </div>
</template>

<script type="text/ecmascript-6">
import { getSingerList } from 'api/singer'
import { ERR_OK } from 'api/config'
import Singer from 'common/js/singer'
import ListView from 'base/listview/listview'
import { mapMutations } from 'vuex'
import { playlistMixin } from 'common/js/mixin'

// 最大热门歌手数量
const HOT_SINGER_LEN = 10

export default {
    mixins: [ playlistMixin ],
    data() {
        return {
            singers: []
        }
    },
    components: {
        ListView
    },
    created() {
        this._getSingerList()
    },
    methods: {
        // 给底部预留给mini播放器的空间
        handlePlaylist(playlist) {
            const bottom = playlist.length > 0 ? '60px' : ''
            this.$refs.singer.style.bottom = bottom
            this.$refs.list.refresh()
        },
        selectedSinger(singer) {
            this.$router.push({
                path: `/singer/${singer.id}`
            })
            this.setSinger(singer)
        },

        _getSingerList() {
            getSingerList().then((res) => {
                if (res.code === ERR_OK) {
                    this.singers = this._normalizeSinger(res.data.list)
                }
            })
        },

        _normalizeSinger(list) {

            // 存储歌手的数据
            let map = {
                hot: {
                    title: '热门',
                    items: []
                }
            }
            list.forEach((item, index) => {
                if ( index < HOT_SINGER_LEN ) {
                    map.hot.items.push(new Singer({
                        id: item.Fsinger_mid,
                        name: item.Fsinger_name
                    }))
                }

                // key为歌手名字首字母
                const key = item.Findex

                // 如果没有该首字母就先创建一个对象存储首字母
                if (!map[key]) {
                    map[key] = {
                        title: key,
                        items: []
                    }
                }
                map[key].items.push(new Singer({
                    id: item.Fsinger_mid,
                    name: item.Fsinger_name
                }))
            })

            // 因为Map的数据是无序的，需要重新排序
            let hot = []
            let ret = []

            // 遍历map的数据
            for (let key in map) { 
                let val = map[key]
                // 如果有匹配到字母，则放进ret里面，是热门字符就放进热门
                if (val.title.match(/[a-zA-Z]/)) {
                    ret.push(val)
                } else if (val.title === '热门') {
                    hot.push(val)
                }
            }
            // 进行排序操作，根据字母的Unicode编码排序
            ret.sort((a, b) => {
                return a.title.charCodeAt(0) - b.title.charCodeAt(0)
            })
            // 因为是一维数组，所以返回hot数组加ret数组
            return hot.concat(ret)
        },

        ...mapMutations({
            setSinger: 'SET_SINGER'
        })
    }
}
</script>

<style lang="stylus" scoped rel="stylesheet/stylus">
.singer
    position: fixed
    top: 88px
    bottom: 0
    width: 100%
</style>